﻿using System.Collections.Generic; 
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine; 

namespace XFGameFramework.Localization
{
    public class LanguageSearchWindow : EditorWindow
    {

        private SerializedProperty id;
         
        private LanguageSearchListTree languageSearchListTree;

#if UNITY_6000_2_OR_NEWER
        private TreeViewState<int> treeViewState;
#else
        private TreeViewState treeViewState;
#endif
        private MultiColumnHeaderState multiColumnHeaderState;

        private Rect listRect;

        private string searchStr;

        private GUIContent currentLanguage;

        private MultiLanguageType multiLanguageType;

        public SystemLanguage language;


        private void Awake()
        {
            titleContent = new GUIContent("选择配置","请选择要使用的语言配置!");
        }

        private void OnEnable()
        {
            language = LocalizationManager.Language;
        }

        public void Init(SerializedProperty id, MultiLanguageType multiLanguageType)
        {
            this.id = id;
            this.multiLanguageType = multiLanguageType;

            EditorApplication.delayCall += () =>
            {
                Focus();
            };

        }

        private void OnGUI()
        {

            GUILayout.BeginHorizontal();
            // 
            
            searchStr = EditorGUILayout.TextField(string.Empty,searchStr, "SearchTextField");

            if (currentLanguage == null)
                currentLanguage = new GUIContent();

            currentLanguage.text = language.ToString(); 

            if (EditorGUILayout.DropdownButton(currentLanguage, FocusType.Passive, EditorStyles.toolbarDropDown, GUILayout.Width(this.position.width * 0.35f * 0.3f)))
            {
                GenericMenu menu = new GenericMenu();

                Dictionary<SystemLanguage, string> Languages = new Dictionary<SystemLanguage, string>();

                foreach (var item in LocalizationManager.Configs)
                {
                    foreach (var language in item.Languages)
                    {
                        if (Languages.ContainsKey(language.language))
                            continue;
                        Languages.Add(language.language, string.IsNullOrEmpty(language.displayName) ? language.language.ToString() : language.displayName);
                    }
                }

                foreach (SystemLanguage language in Languages.Keys)
                {
                    string display = language.ToString();
                    if (!string.IsNullOrEmpty(Languages[language]))
                    {
                        display = string.Format("{0}({1})", display, Languages[language]);
                    }

                    menu.AddItem(new GUIContent(display), this.language == language, () => {
                        this.language = language;
                        languageSearchListTree.Reload();
                    });
                }

                menu.ShowAsContext();
            }

            GUILayout.EndHorizontal();


            if (treeViewState == null) 
            {
#if UNITY_6000_2_OR_NEWER 
                treeViewState = new TreeViewState<int>();
#else
                treeViewState = new TreeViewState();
#endif
            }



            if (multiColumnHeaderState == null) {

                var headerState = LanguageSearchListTree.CreateDefaultMultiColumnHeaderState();
                if (MultiColumnHeaderState.CanOverwriteSerializedFields(multiColumnHeaderState, headerState))
                    MultiColumnHeaderState.OverwriteSerializedFields(multiColumnHeaderState, headerState);
                multiColumnHeaderState = headerState;
            }

            if (languageSearchListTree == null)
            {
                languageSearchListTree = new LanguageSearchListTree(treeViewState, multiColumnHeaderState, id,this,multiLanguageType);
                languageSearchListTree.Reload();
            }


            listRect.Set(0,20,position.width,position.height-20);
            languageSearchListTree.searchString = searchStr;

            languageSearchListTree.OnGUI(listRect); 
        }


        private void OnLostFocus()
        {
            Close();
        }

    }

}

